<template>
    <el-card style="margin: 10px;">
        <div slot="header">
            <h2>
                批量新增货品
            </h2>
        </div>
        <el-row style="padding-bottom:10px">
            <el-button type="danger" size="small" @click="handleCommonServer">
                公共货品库
            </el-button>
            <el-select size="small" v-model="businessId" placeholder="请选择商户" filterable clearable @change="businessChange">
                <el-option v-for="item in businessList" :value="item.id" :label="item.name" :key="item.id">

                </el-option>
            </el-select>
            <el-upload style="display: inline-block;" action="#" :on-change="handleBatchImport" :show-file-list="false"
                :disabled="!businessId ? true : false"
                accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                :auto-upload="false">
                <el-button icon="el-icon-plus" size="small" plain type="success"
                    @click="handleBatchImportTip">批量导入货品</el-button>
            </el-upload>
            <div class="tip">仅支持导入货品类别为普通且单规格的货品</div>
        </el-row>
        <el-table :data="goods" :header-cell-style="{ 'background-color': '#F2F2F2', 'color': 'black' }" max-height="700px">
            <el-table-column label="操作" width="110" align="center">
                <template slot-scope="scope">
                    <el-button v-if="scope.$index == goods.length - 1" icon="el-icon-plus" type="success" plain
                        size="medium" circle @click="addBtn(scope.$index)">
                    </el-button>
                    <el-button v-if="goods.length != 1" icon="el-icon-minus" type="danger" plain size="medium"
                        @click="subBtn(scope.$index)" circle>
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column align="center" width="230">
                <template slot-scope="scope">
                    <el-cascader v-model="scope.row.departIds" filterable clearable placeholder="请选择部门"
                        :options="departList" :props="{ value: 'departId', label: 'departName', checkStrictly: true }">

                    </el-cascader>
                </template>
                <template slot="header" slot-scope="scope">
                    <div class="table-header">
                        部门<el-button type="danger" size="mini" @click="handleCopy('departIds')">复制</el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column align="center" width="190">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.ghModelId" filterable clearable>
                        <el-option v-for="item in goodsHoursList" :value="item.modelId" :label="item.modelName"
                            :key="item.modelId">

                        </el-option>
                    </el-select>
                </template>
                <template slot="header" slot-scope="scope">
                    <div class="table-header">
                        可售时间模板<el-button type="danger" size="mini" @click="handleCopy('ghModelId')">复制</el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="货品名称" width="190" align="center">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.name" placeholder="请输入货品名称" clearable></el-input>
                </template>
            </el-table-column>
            <el-table-column label="货品分类" width="200" align="center">
                <template slot-scope="scope">
                    <el-cascader v-model="scope.row.classifyIds" :options="treeThreeServerList" placeholder="请选择货品分类"
                        :props="{ label: 'classifyName', value: 'serverClassifyId' }" clearable>
                    </el-cascader>
                </template>
            </el-table-column>
            <el-table-column label="商户分类" align="center" width="200">
                <template slot-scope="{row,$index}">
                    <el-select :class="['business-classify_select', 'business-classify_select' + $index]"
                        @change="handleBusinessClassifyChange($index)" v-model="row.businessClassifyIds" clearable
                        filterable multiple>
                        <el-option v-for="item in businessClassifyList" :value="item.id" :label="item.name" :key="item.id">
                            <div class="business-classify_name" v-html="item.name">
                            </div>
                        </el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="货品简介" width="210" align="center">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.introduce" placeholder="请输入货品简介" clearable></el-input>
                </template>
            </el-table-column>
            <el-table-column label="货品描述" width="210" align="center">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.describes" placeholder="请输入货品描述" clearable></el-input>
                </template>
            </el-table-column>
            <el-table-column label="货品图片" align="center" width="120">
                <template slot-scope="scope">
                    <upload-img :index="scope.$index" class="upload-img" @uploadFileList="getFileUrl" folder="serverImg">
                        <img width="100%" v-if="scope.row.mainImage" :src="scope.row.mainImage" alt="">
                        <i v-if="!scope.row.mainImage" class="el-icon-plus"></i>
                    </upload-img>
                    <el-button style="margin-top: 4px;" type="primary" size="mini"
                        @click="showImageSelectDialog = true; index = scope.$index">
                        公共图库
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column label="商户确认方式" width="150px" align>
                <template slot="header" slot-scope="scope">
                    <div class="table-header">
                        商户确认方式<el-button type="danger" size="mini" @click="handleCopy('businessConfirmWay')">复制</el-button>
                    </div>
                </template>
                <template slot-scope="{row}">
                    <el-select v-model="row.businessConfirmWay">
                        <el-option v-for="item in businessConfirmWayList" :label="item.name" :value="item.id"
                            :key="item.id"></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="是否上门" width="100px" align="center">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.visitFlag">
                        <el-option :value="1" label="是"></el-option>
                        <el-option :value="0" label="否"></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="是否到店" width="100px" align="center">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.shopFlag">
                        <el-option :value="1" label="是">
                        </el-option>
                        <el-option :value="0" label="否">
                        </el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="是否远程" width="100px" align="center">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.remoteFlag">
                        <el-option :value="1" label="是"></el-option>
                        <el-option :value="0" label="否"></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="紧急通知" width="100px" align="center">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.fastFlag">
                        <el-option :value="1" label="是"></el-option>
                        <el-option :value="0" label="否"></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="是否需配送费" width="100px" align="center">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.deliveryFlag">
                        <el-option :value="1" label="是"></el-option>
                        <el-option :value="0" label="否"></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="平台原价" align="center" width="150">
                <template slot-scope="scope">
                    <el-input type="number" v-model.number="scope.row.price"
                        @input="handleMoneyInputList(scope.row, 0)"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="平台售价" align="center" width="150">
                <template slot-scope="scope">
                    <el-input type="number" v-model.number="scope.row.salePrice"
                        @input="handleMoneyInputList(scope.row, 1)"></el-input>
                </template>
            </el-table-column>

            <el-table-column label="商户原价" align="center" width="125">
                <template slot-scope="scope">
                    <el-input type="number" v-model.number="scope.row.marketPrice"
                        @input="handleMoneyInputList(scope.row, 5)" @blur="handleMarketPriceBlur(scope.row)">
                    </el-input>
                </template>
            </el-table-column>
            <el-table-column label="商户折扣比例" align="center" width="125">
                <template slot-scope="scope">
                    <el-input type="number" v-model.number="scope.row.discount" @blur="handleDiscountBlur(scope.row)"
                        @input="handleMoneyInputList(scope.row, 4)">
                        <span slot="suffix" style="line-height: 40px;">%</span>
                    </el-input>
                </template>
            </el-table-column>
            <el-table-column label="成本价" align="center" width="150">
                <template slot-scope="scope">
                    <el-input type="number" v-model.number="scope.row.costPrice" @input="handleMoneyInputList(scope.row, 2)"
                        @blur="handleCostBlur(scope.row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="包装费" align="center" width="150">
                <template slot-scope="scope">
                    <el-input type="number" v-model.number="scope.row.packageFee"
                        @input="handleMoneyInputList(scope.row, 3)"></el-input>
                </template>
            </el-table-column>
            <el-table-column label="包装费计算方式" align="center" width="150">
                <template slot-scope="{row}">
                    <el-select v-model="row.packageFlag">
                        <el-option :value="1" label="独立包装费"></el-option>
                        <el-option :value="2" label="合并包装费"></el-option>
                    </el-select>
                </template>
            </el-table-column>

            <el-table-column label="库存" align="center" width="155">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.stock" @input="handleMoneyInputList(scope.row, 6)">
                    </el-input>
                </template>
            </el-table-column>
            <el-table-column label="是否对接" align="center" width="130">
                <template slot-scope="{row,$index}">
                    <el-select v-model="row.dockingFlag"
                        @change="row.dockingType = ''; row.doctorId = ''; row.itemCode = ''">
                        <el-option :value="1" label="非对接" key="1"></el-option>
                        <el-option :value="2" label="对接" key="2"></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="对接类型" align="center" width="140">
                <template slot-scope="{row}">
                    <el-select v-model="row.dockingType" v-if="row.dockingFlag == 2"
                        @change="row.doctorId = ''; row.itemCode = ''">
                        <el-option v-for="item in $Options.dockingTypeList" :value="item.id" :label="item.name"
                            :key="item.id"></el-option>
                    </el-select>
                    <span v-else>
                        #
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="关联医生" align="center" width="180">
                <template slot-scope="{row}">
                    <el-select v-model="row.doctorId" v-show="row.dockingType == 1">
                        <el-option v-for="item in row.doctorList" :value="item.id" :label="item.name"
                            :key="item.id"></el-option>
                    </el-select>
                    <span v-show="row.dockingType != 1">
                        #
                    </span>

                </template>
            </el-table-column>
            <el-table-column label="项目编码" align="center" width="180">
                <template slot-scope="{row}">
                    <el-input v-model="row.itemCode" v-show="row.dockingType == 2"></el-input>
                    <span v-show="row.dockingType != 2">
                        #
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="项目名称" align="center" width="180">
                <template slot-scope="{row}">
                    <el-input v-model="row.itemName" v-show="row.dockingType == 2"></el-input>
                    <span v-show="row.dockingType != 2">
                        #
                    </span>
                </template>
            </el-table-column>
        </el-table>
        <div class="bottom">
            <el-button plain type="primary" @click="sureClick">保 存</el-button>
            <el-button plain @click="returnList">取 消</el-button>
        </div>
        <CommonServerSelect :businessId="businessId" ref="commonServer" @getServerList="getServerList" :variantState="1">
        </CommonServerSelect>

        <ImageSelect :show="showImageSelectDialog" folder="serverImg" :limit="1" @cancel="showImageSelectDialog = false"
            @getUrlList="getUrlList"></ImageSelect>
    </el-card>
</template>

<script>
import ServerClassifyApi from "@/api/server/serverClassify";
import UploadImg from "@/components/UploadImg.vue";
import CommonServerSelect from "@/components/CommonServerSelect"
import BusinessServerApi from "@/api/goods/businessServer"
import BusinessDepartApi from "../../api/business/depart";
import BusinessApi from "@/api/business/business";
import TimeTemplateApi from '@/api/goods/timeTemplate';
import ImageSelect from "../../components/ImageSelect.vue";
import importExcel from "@/excel/importExcel";
import DoctorApi from "@/api/institution/doctor";
export default {
    props: {
        show: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            businessId: "",
            businessList: [],
            goodsHoursList: [],
            goods: [
                {
                    // serverClassifyId: "",
                    // thirdClassifyId: "",
                    // thirdClassifyName: "",
                    // threeLevelClassList: [], //三级服务分类列表，不需要提交
                    businessClassifyIds: [],
                    classifyIds: [],
                    name: "",
                    departIds: [],
                    mainImage: "",
                    introduce: "",
                    describes: "",
                    businessConfirmWay: 1,
                    visitFlag: 1,//是否上们，1代表是
                    shopFlag: 0,//是否到店，1代表是，0代表不是
                    remoteFlag: 0,//是否远程，1代表是，0代表不是
                    fastFlag: 0,//紧急通知
                    deliveryFlag: 1,//是否需配送费
                    price: "",
                    salePrice: "",
                    costPrice: "",
                    packageFee: "", //包装费
                    packageFlag: 1,
                    discount: "",
                    ghModelId: "",
                    doctorId: "",
                    itemCode: "",
                    itemName: "",
                    doctorList: [],
                    dockingFlag: 1,// 1非对接 2对接
                    dockingType: "",
                }
            ],
            departList: [],
            treeThreeServerList: [],
            businessClassifyList: [],
            businessConfirmWayList: [
                { id: 1, name: '手动确认' },
                { id: 2, name: "自动确认" },
                { id: 3, name: "支付后自动确认" }
            ],
            index: "",
            showImageSelectDialog: false,
        }
    },
    components: {
        UploadImg,
        CommonServerSelect,
        ImageSelect
    },
    mounted() {

        this.getBusinessList()
        // this.getDepartList()
    },
    methods: {
        addBtn(index) {
            let addObj = this.goods[index]
            this.goods.push({
                businessClassifyIds: addObj.businessClassifyIds || [],
                classifyIds: addObj.classifyIds || [],
                ghModelId: addObj.ghModelId || "",
                name: "",
                departIds: addObj.departIds || [],
                mainImage: addObj.mainImage,
                introduce: addObj.introduce,
                describes: addObj.describes,
                businessConfirmWay: addObj.businessConfirmWay,
                visitFlag: addObj.visitFlag,//是否上们，1代表是
                shopFlag: addObj.shopFlag,//是否到店，1代表是，0代表不是
                remoteFlag: addObj.remoteFlag,//是否远程，1代表是，0代表不是
                fastFlag: addObj.fastFlag,//紧急通知
                deliveryFlag: addObj.deliveryFlag,//是否需配送费
                price: "",
                salePrice: "",
                costPrice: "",
                packageFee: "",
                packageFlag: 1,
                discount: "",
                doctorId: "",
                itemCode: "",
                itemName: "",
                doctorList: [],
                dockingFlag: 1,
                dockingType: '',
            })
            this.handleBusinessClassifyChange(this.goods.length - 1)
        },
        subBtn(index) {
            this.goods.splice(index, 1)
            this.handleBusinessClassifyChange()
        },
        handleCopy(prop) {
            this.goods.forEach(item => this.$set(item, prop, this.goods[0][prop]))
        },
        businessChange() {
            this.goods.forEach(item => {
                item.ghModelId = ""
                item.classifyIds = []
                item.departIds = []
                item.businessClassifyIds = []
                item.doctorId = ""
                item.doctorList = []
            })
            this.treeThreeServerList = []
            this.goodsHoursList = []
            this.departList = []
            this.businessClassifyList = []
            if (!this.businessId) return
            this.getTreeThreeServerList()
            this.getBusinessServerHoursList()
            this.getBusinessClassifyList()
            this.getDepartList()
        },
        getBusinessServerHoursList() {
            TimeTemplateApi.listIdName({ businessId: this.businessId }).then(res => {
                this.goodsHoursList = res.data
            })
        },
        getBusinessClassifyList() {
            BusinessServerApi.classifyListIdName({ businessId: this.businessId }).then(res => {
                this.businessClassifyList = res.data
            })
        },
        // 商户分类改变
        handleBusinessClassifyChange(index, add = false) {
            if (typeof index == 'number') {
                if (!add) {
                    this.goods[index].doctorId = ""
                }
                this.goods[index].doctorList = []
                this.getDoctorList(this.goods[index])
            }
            this.$nextTick(() => {
                setTimeout(() => {
                    // 这里需要添加一个异步
                    this.goods.forEach((item, index) => {
                        const els = document.querySelectorAll(`.business-classify_select${index} .el-select__tags-text`)
                        item.businessClassifyIds.forEach((item2, index2) => {
                            const obj = this.businessClassifyList.find(item3 => item3.id == item2)
                            els[index2].innerHTML = obj.name
                        })
                    })
                }, 50)

            })
        },
        async getDoctorList(goods) {
            // const {businessClassifyList} = this
            if (!goods.businessClassifyIds.length) return
            let businessClassifyIds = goods.businessClassifyIds.toString()
            const { data } = await DoctorApi.getDoctorNames({ businessClassifyIds })
            goods.doctorList = data || []
        },
        // 获取货品图片路径
        getFileUrl(val) {
            if (val.uploadFileList.length > 0) {
                this.$Utils.notifySuccess("上传成功");
            }
            this.goods[val.index].mainImage = val.uploadFileList[0]
        },
        getUrlList(val) {
            this.goods[this.index].mainImage = val[0].url
            this.showImageSelectDialog = false
        },
        // 获取二三级服务列表
        getTreeThreeServerList() {
            let params = {
                level: 2,
                businessId: this.businessId,
            }
            ServerClassifyApi.listServerClassify(params).then(res => {
                this.treeThreeServerList = res.data
            })
        },
        // 获取商户列表
        getBusinessList() {
            BusinessApi.listIdNameVo().then(res => {
                this.businessList = res.data
            })
        },
        // 获取部门列表
        getDepartList() {
            BusinessDepartApi.listMangerTree({ businessId: this.businessId }).then(res => {
                this.setDisabled(res.data)
                this.departList = res.data
            })
        },
        setDisabled(list) {
            list.forEach(item => {
                if (!item.power) {
                    item.disabled = true
                }
                if (item.children) {
                    this.setDisabled(item.children)
                }

            })
        },
        // 列表价格、配送费、包装费的输入限制
        handleMoneyInputList(row, flag) {
            // flag为0表示原价，为1表示配送费，为2表示包装费,3表示商户折扣比例
            if (!flag) {
                // 只保留两位小数
                row.price = parseFloat(parseFloat(row.price).toFixed(2))
                !row.price ? row.price = 0 : ""
            } else if (flag == 1) {
                row.salePrice = parseFloat(parseFloat(row.salePrice).toFixed(2))
                !row.salePrice ? row.salePrice = 0 : ""
            } else if (flag == 2) {
                row.costPrice = parseFloat(parseFloat(row.costPrice).toFixed(4))
                !row.costPrice ? row.costPrice = 0 : ""
            } else if (flag == 3) {
                row.packageFee = parseFloat(parseFloat(row.packageFee).toFixed(2))
                !row.packageFee ? row.packageFee = 0 : ""
            } else if (flag == 4) {
                row.discount = parseFloat(parseFloat(row.discount).toFixed(2))
                if (row.discount && row.discount > 99.72) {
                    row.discount = 0
                    this.$message.error("商户折扣比例只能是0-99.72之间")
                } else if (row.discount) {
                    row.discount = parseFloat(parseFloat(row.discount).toFixed(2))
                } else {
                    row.discount = 0
                }
            } else if (flag == 5) {
                row.marketPrice = parseFloat(parseFloat(row.marketPrice).toFixed(2))
                !row.marketPrice ? row.marketPrice = "" : ""
            } else if (flag == 6) {
                row.stock = row.stock.replace(/[^0-9]|^0/g, "")
            }
        },
        // 商户原价改变
        handleMarketPriceBlur(row) {
            const { marketPrice, discount, costPrice } = row
            if (costPrice && marketPrice && costPrice > marketPrice) {
                row.marketPrice = ""
                row.discount = ""
                this.$message.error("商户原价不能小于成本价")
                return
            } else if (marketPrice && discount) {
                row.costPrice = parseFloat((marketPrice * discount / 100).toFixed(4))
            }


        },
        // 商户折扣比例改变
        handleDiscountBlur(row) {
            const { marketPrice, discount } = row
            if (marketPrice && discount) {
                row.costPrice = parseFloat((marketPrice * discount / 100).toFixed(4))
            }
        },
        handleCostBlur(row) {
            const { costPrice, marketPrice } = row
            if (costPrice > marketPrice) {
                row.marketPrice = ""
                row.discount = ""
                this.$message.error("成本价不能大于商户原价")
                return
            } else if (costPrice && marketPrice) {
                row.discount = parseFloat((costPrice / marketPrice * 100).toFixed(2))
            }

        },
        // 公共货品库
        handleCommonServer() {
            if (!this.businessId) {
                this.$message.error("请选择商户")
                return
            }
            this.$refs.commonServer.handleCreate()
        },
        getServerList(val) {
            val.list.forEach(async item => {
                const departIds = []
                val.departId ? departIds.push(val.departId) : ""
                val.firstDepartId ? departIds.push(val.firstDepartId) : ""
                val.secondDepartId ? departIds.push(val.secondDepartId) : ""
                val.thirdDepartId ? departIds.push(val.thirdDepartId) : ""
                this.goods.push({
                    packageFlag: item.packageFlag,
                    businessClassifyIds: item.businessClassifyIds || [],
                    classifyIds: item.classifyIds || [item.serverClassifyId, item.thirdClassifyId],
                    name: item.name,
                    departIds: departIds,
                    mainImage: item.mainImage,
                    describes: item.describes,
                    introduce: item.introduce,
                    visitFlag: item.visitFlag,//是否上们，1代表是
                    shopFlag: item.shopFlag,//是否到店，1代表是，0代表不是
                    remoteFlag: item.remoteFlag,//是否远程，1代表是，0代表不是
                    fastFlag: item.fastFlag,//紧急通知
                    deliveryFlag: item.deliveryFlag,//是否需配送费
                    price: item.price,
                    salePrice: item.price,
                    costPrice: item.costPrice,
                    packageFee: item.packageFee,
                    discount: item.discount,
                    stock: item.stock,
                    marketPrice: item.marketPrice,
                    ghModelId: item.ghModelId,
                    doctorId: item.doctorId ? Number(item.doctorId) : "",
                    itemCode: item.itemCode || "",
                    itemName: item.itemName || "",
                    doctorList: [],
                    dockingFlag: item.dockingFlag || 1,
                    dockingType: item.dockingType || ""
                })
                this.handleBusinessClassifyChange(this.goods.length - 1, true)
            })
            console.log(this.goods)
        },
        async handleBatchImport(file) {
            let res = await importExcel(file, 0, ["name", "classifyIds", "businessClassifyIds", "introduce", "describes", "mainImage", "visitFlag", "shopFlag", "remoteFlag",
                "fastFlag", "deliveryFlag", "price", "salePrice", "marketPrice", "discount", "costPrice", "packageFee", "packageFlag", "stock", 'dockingFlag', 'dockingType', "doctorId", "itemCode", "itemName"
            ])
            res.forEach(item => {
                if (item.classifyIds) item.classifyIds = item.classifyIds.split(":").map(item => Number(item))
                if (item.businessClassifyIds) item.businessClassifyIds = String(item.businessClassifyIds).split(":").map(item => Number(item))
            })
            this.getServerList({ list: res })

        },
        handleBatchImportTip() {
            if (!this.businessId) return this.$message.error("请先选择商户")
        },
        checkData() {
            if (!this.businessId) {
                this.$message.error("请选择商户")
                return
            }
            let flag = this.goods.find(item => {
                if (item.classifyIds.length == 0) {
                    this.$message.error("请填写分类")
                    return true
                }
                if (!item.name) {
                    this.$message.error("请填写货品名称")
                    return true
                }
                if (!item.name) {
                    this.$message.error("请选择货品图片")
                }
                if (!item.price && parseInt(item.price) !== 0) {
                    this.$message.error("请填写平台原价")
                    return true
                }
                if (!item.salePrice && parseInt(item.salePrice) !== 0) {
                    this.$message.error("请填写平台售价")
                    return true
                }
                if (!item.costPrice && parseInt(item.costPrice) !== 0) {
                    this.$message.error("请填写成本价")
                    return true
                }
                if (!item.marketPrice && parseInt(item.marketPrice) !== 0) {
                    this.$message.error("请填写商户原价")
                    return true
                }
                if (!item.discount && parseInt(item.discount) !== 0) {
                    this.$message.error("请填写商户折扣比例")
                    return true
                }

                if (item.dockingFlag == 2 && !item.dockingType) {
                    this.$message.error("请选择对接类型")
                    return true
                }

            })



            if (flag) return

            return true
        },
        sureClick() {
            if (!this.checkData()) return
            if (this.goods.length > 0) {
                const nameValues = this.goods.map(item => item.name); // 提取所有 a 的值
                const uniqueAValues = new Set(nameValues); // 创建一个 Set，用于存储唯一值
                if (uniqueAValues.size < nameValues.length) {
                    this.$message.error("货品名称不能重复")
                } else {
                    BusinessApi.isNameRepeat({ businessId: this.businessId, nameList: nameValues }).then(res => {
                        if (res.data.length !== 0) {
                            this.$message.error(`以下货品名称出现重复"${res.data}"`)
                        } else {
                            this.$confirm("确认保存吗？", '提示', {}).then(() => {
                                let arr = []
                                this.goods.forEach(item => {
                                    let obj = { ...item }
                                    obj.serverClassifyId = obj.classifyIds[0]
                                    obj.thirdClassifyId = obj.classifyIds[1]
                                    obj.departId = obj.departIds[0] || ""
                                    obj.firstDepartId = obj.departIds[1] || ""
                                    obj.secondDepartId = obj.departIds[2] || ""
                                    obj.thirdDepartId = obj.departIds[3] || ""
                                    delete obj.classifyIds
                                    delete obj.departIds
                                    delete obj.doctorList
                                    arr.push(obj)
                                })
                                let params = {
                                    params: arr,
                                    businessId: this.businessId
                                }
                                BusinessServerApi.batchAddGoods(params).then(res => {
                                    this.$Utils.notifySuccess('保存成功')
                                    this.returnList()
                                })
                            })
                        }
                    })
                }
            }


        },
        returnList() {
            this.$store.dispatch('delView', this.$route);
            this.$router.back();
        }
    }
}
</script>
<style lang="scss" scoped>
.el-card {
    &::v-deep .el-card__header {
        font-size: 12px;
        padding: 6px 20px;
    }
}

.el-table {
    font-size: 12px;
    border: 1px solid #E6E6E6;
}

.bottom {
    text-align: center;
    padding: 20px 0;
}

.upload-img {
    &::v-deep .el-upload-dragger {
        width: 100px;
        height: 70px;
        display: flex;
        justify-content: space-around;
        align-items: center;
    }

    .el-icon-plus {
        font-size: 24px;
        vertical-align: -24px;
    }
}

.tip {
    color: red;
    display: inline-block;
    align-self: end;
    margin-left: 10px;
}

.table-header {
    .el-button {
        height: 20px;
        line-height: 20px;
        padding: 0 5px;
        margin-left: 10px;
    }
}

.business-classify_name,
.business-classify_select {
    p {
        margin: 0;

        img {
            vertical-align: middle;
            height: 16px;
            width: auto;
        }
    }
}
</style>
